/*
*
*	@author: alex smoljanovic - alexrs@gmail.com
*	@website: http://code.google.com/p/accordian-ars/
*	@date: 7/20/2012
*	
*	Accordian Widget
*	
*	behaviour inspecific:
*		togglerlabel: 'string' - if not false, displays a toggler link
*		behaviour: 'hover' | 'click' - Expanansions are invoked based on this!
*    
*	hover specific:
*		sticky: true = Item will stay open until another item is expanded, false - Item closes on mouseleave
*    
*	click specific:
*		multiple: true = Accordian may have multiple items expanded, false = only one item will be open at a time
*	
*/

	if( !AccordianHelper ) 
	var AccordianHelper = { //global, can mutate
		
		animProp: function(){return { duration: "slow",  queue: false }},
		
		animLabelShown: { paddingTop:'10px' },
		animLabelHidden: { paddingTop:'7px' },
		
		animContentsShown: function(){ return { paddingTop:'10px', paddingBottom:'10px', height: '100%', opacity: '1' }},
		animContentsHidden: function(){ return{ paddingTop:'0px', paddingBottom:'0px', height: '0px', opacity: '.1' }},
		
		hideItem: function( $item )
		{
			$item.removeClass('active');
			$item.find('.label').animate( AccordianHelper.animLabelHidden, AccordianHelper.animProp() );
			var tmp = AccordianHelper.animContentsHidden();

			$item.find('.contents').data('height', $item.find('.contents').height() + 'px');
			
			$item.find('.contents').animate( tmp, AccordianHelper.animProp() );
		},
		
		showItem: function( $item )
		{
			$item.addClass('active');
			$item.find('.label').animate( AccordianHelper.animLabelShown, AccordianHelper.animProp() );
			
			
			var tmp = AccordianHelper.animContentsShown();
			tmp.height = $item.find('.contents').data('height');
			
			var tmpAP = AccordianHelper.animProp();
			tmpAP.complete = function(){ $item.find('.contents').css('height', '').data('height', $item.find('.contents').height() + 'px' ); }
			$item.find('.contents').animate( tmp, tmpAP );
			
			
			
		
		},
		
		toggleItem: function( $item )
		{
			//used when behavior is click..
			if( !$item.hasClass('active' ) )
				AccordianHelper.showItem( $item );
			else
				AccordianHelper.hideItem( $item );
		
		}
	};
   
	var AccordianWidget = function( $el, options, handlers )
	{
	
		if( !options )
			options =
			{
				togglerlabel: 'Show/Hide',
				sticky: true,
				behaviour: 'click'
			};
		
		if( !$el )
			return false;
		
		
		var obj =
		{
			$el: $el,
			options: options,
			onHideItem: handlers && handlers.onHideItem ? handlers.onHideItem : function(label){}, //func (label), override this...
			onShowItem: handlers && handlers.onShowItem ? handlers.onShowItem : function(label){} //func (label), override this...
		};
		
		$el.children().addClass( 'item' );
		
		if( options.togglerlabel )
		{
			$toggler = $('<p class="toggler">' + options.togglerlabel + '</p>').insertBefore( $el );
			$toggler.css('cursor', 'pointer');
			$toggler.click( function() {
				$el.find('li').toggle(
					{ duration: 500, queue: false }
				);
			});
		}
		
		$el.find('li').each( function()
		{
			
			$(this).find('.contents').data('height', $(this).find('.contents').height() + 'px' );
			$(this).find('.contents').css('height', '0px').css('overflow', 'hidden');
			
			if( !options.behaviour || options.behaviour == 'hover' )
				$(this).find('.label').css('cursor', 'default');
			else
				$(this).find('.label').css('cursor', 'pointer');
			
			if( !options.behaviour || options.behaviour == 'hover' )
				$(this).mouseenter( function()
				{
					//hide current
					if( options.sticky )
						$el.find('li.active').each( function()
						{
							AccordianHelper.hideItem( $(this) );
						} );
					
					AccordianHelper.showItem( $(this) );
					obj.onShowItem( $(this).find('.label').text()  );
				});
			
			
			if( options.behaviour == 'click' )
			{
				var $li_this = $(this);
				$(this).find('.label').click( function()
				{
					//var $li_this = $li_this; //scoped...
					var tmpActive = $li_this.hasClass('active');
					//hide current
					if( !options.multiple )
						$el.find('li.active').each( function()
						{
							AccordianHelper.hideItem( $(this) );
							obj.onHideItem( $(this).find('.label').text()  );
						} );
					
					if( tmpActive )
						AccordianHelper.showItem( $li_this );
					
					AccordianHelper.toggleItem( $li_this );
					
					if( $li_this.hasClass('active') )
						obj.onShowItem( $li_this.find('.label').text()  );
					else
						obj.onHideItem( $li_this.find('.label').text()  );
					
				});
			}
			
			
			if( !options.behaviour || options.behaviour == 'hover' )
				if( !options.sticky )
					$(this).mouseleave( function()
					{
						AccordianHelper.hideItem( $(this) );
						obj.onHideItem( $(this).find('.label').text()  );
					});
		});
		
		//expand default, if there is one...
		if( options.sticky || options.behaviour == 'click' )
			$el.find('li.default').each( function()
			{
				AccordianHelper.showItem( $(this) );
				obj.onShowItem( $(this).find('.label').text()  );
			});
		
		
		
		
		return obj;
	
	};